package com.dj.product.k2faas.converter;

import jakarta.persistence.AttributeConverter;
import com.dj.product.k2faas.common.utils.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * Created by 丁建 on 2025/9/23 08:39
 *
 * @author 丁建
 */
public abstract class AbstractNullableListObjectConverter<X> implements AttributeConverter<List<X>, String> {
    @Override
    public String convertToDatabaseColumn(List<X> object) {
        if (object == null) {
            return "[]";
        }
        return JsonUtil.toJSON(object);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<X> convertToEntityAttribute(String s) {
        if (isBlank(s)) {
            return new ArrayList<>();
        }
        return (List<X>) JsonUtil.toList(s, getClazz());
    }

    protected abstract Class<?> getClazz();
}
